<?php
session_start();

if(isset($_SESSION["type"]))
	$tipo = $_SESSION["type"];

$from = @$_GET["from"];

$chiave = @$_POST["chiave"];
$messaggio = @$_POST["messaggio"];

if($from == "")
	$from = "pubblicati";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<style type="text/css" media="print">
TD {font-size:11px; font-family="arial,helvetica"}
P {text-indent:-10; text-align="justify"; margin-left:10;}
A:link {color: #44aaaa; text-decoration: underline;}
A:active {color: #000000; text-decoration: underline;}
A:visited {color: #227788; text-decoration: underline;}
A:hover {color: #000000; text-decoration: none; }
BODY {font-size:12px; font-family="arial,helvetica";}
FORM {margin-top:5}
</style>
<style type="text/css" media="screen">
TD {font-size:13px}
P {text-indent:-10; text-align="justify"; margin-left:10;}
A:link {color: #44aaaa; text-decoration: underline;}
A:active {color: #000000; text-decoration: underline;}
A:visited {color: #227788; text-decoration: underline;}
A:hover {color: #000000; text-decoration: none;}
BODY {background-image: url(images/captext.gif); font-size:14px}
FORM {margin-top:5}
</style>
	
<script type="text/javascript" language="javascript">
	function ctrl()
	{
		var frm = document.getElementById("search");
		
		if(frm.chiave.value == "")
		{
			alert("Attenzione, compilare tutti i campi del modulo.");
			return false;
		}
		
		return true;
	}
</script>
	
</head>

<body>
<center><font color="#228888" size="4"><br><br><b>BACHECA: RICERCA MESSAGGI</b><br><br></font></center>
<center>
<br><br>
<form id="search" name="search" method="post" action="ricerca_msg.php">
	<table style="width: 90%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="2" cellspacing="2">
		<tr><td style="text-align: center;">Ricerca una parola nei messaggi.<br><br></td></tr>
		<tr align="center">
			<table width="280px" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td style="width: 120px; height: 30px;"><b>Chiave di Ricerca:</b></td>
					<td style="height: 30px;"><input name="chiave" size="20" maxlength="20" type="text"></td>
				</tr>
				<tr>
					<td style="width: 120px; height: 30px;"><b>Messaggio:</b></td>
					<td style="height: 30px;">
						<select name="messaggio" id="messaggio">
							<option value="pubblicati">Pubblicati</option>
							<?php if($tipo == "admin") echo "<option value=\"pendenti\">Pendenti</option>\n"; ?>
							<option value="scaduti">Scaduti</option>
						</select>
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td style="width: 120px;">&nbsp;</td>
					<td align="right"><input name="go" type="submit" id="go" value="Cerca" onclick="javascript: return ctrl();"></td>
				</tr>
			</table>
		</tr>
	</table>
</form>
<br>
<?php
switch($messaggio)
{
	case "pubblicati":
		$tabella = "Annuncio_pubblicato";
		break;
	
	case "pendenti":
		$tabella = "Annuncio_pendente";
		break;
	
	case "scaduti":
		$tabella = "Annuncio_scaduto";
		break;
}

require("include/config.inc.php");
require("include/mysql.inc.php");

if($chiave != "")
{	
	$db = new MySQL();

	$db->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_DEBUG);

	$db->query("SELECT * FROM $tabella WHERE (corpo LIKE '%$chiave%') OR (autore  LIKE '%$chiave%') ORDER BY data_pub DESC, codice DESC");

	if($db->get_num_rows() > 0)
	{
		echo "<p>Ricerca: Trovati " . $db->get_num_rows() . " risultati con la parola chiave <b>$chiave</b>.</p>";
		
		while($row = $db->fetch_array())
		{
			$d_pub = date("d/m/Y", strtotime($row["data_pub"]));
			$d_sca = date("d/m/Y", strtotime($row["data_sca"]));
			
			echo "<table style=\"table-layout: fixed; width:90%; margin-left: auto; margin-right: auto;\">
				  <tr bgcolor=\"#FFDFBF\">
					<td><b>Pubblicato in data: <font color=\"#228888\" size=\"3\">$d_pub</font></b></td>
					<td align=\"right\"><b>Autore: </b>" . $row["autore"] . "</td>
					<td align=\"right\"><b>Scadenza: </b>$d_sca</td>
				  </tr>
				  <tr><td colspan=\"3\">" . str_replace("\n", "<br>", $row["corpo"]) . "</td></tr>
				  </table>";
		}
	}
	else
		echo "<p>Ricerca: Trovati " . $db->get_num_rows() . " risultati con la parola chiave <b>$chiave</b>.</p>";

	$db->free_result();
	$db->close();
}
?>
<table style="width: 90%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="2" cellspacing="2">
	<tr><td style="vertical-align: middle; text-align: right;"><a href="<?php echo $from; ?>.php">Indietro</a></td></tr>
</table>
</center>
<br>
<table bgcolor="#88CCCC" border="0" cellpadding="1" cellspacing="0" width="100%">
<tr>
	<td> Ultima modifica: <?php echo date("d/m/Y");?></td>
	<td align="right"> Approvato da: Presidente CCdL</td>
</tr>
</table>
</body>
</html>
